package com.ruoyi.teacher.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.teacher.api.domain.Campus;
import com.ruoyi.teacher.api.domain.TeacherDto;
import com.ruoyi.teacher.service.CampusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 校园中心
 *
 * @author clid
 * @date 2024/03/23
 */
@RestController
@RequestMapping("/campus")
public class CampusController {
    @Autowired
    private CampusService campusService;
    @Autowired
    private RedisService redisService;

    @GetMapping
    R<List<Campus>> loadCampus() {
        if (!redisService.hasKey(CacheConstants.CAMPUS_KEY)) {
            List<Campus> campuses = campusService.loadCampus();
            redisService.setCacheList(CacheConstants.CAMPUS_KEY, campuses);
            return R.ok(campuses);
        }else {
            List<Campus> campuses = BeanUtil.copyToList(redisService.getCacheList(CacheConstants.CAMPUS_KEY), Campus.class);
            return R.ok(campuses);
        }
    }
}
